package vn.com.bookstore.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import vn.com.bookstore.domain.model.AudioBook;
import vn.com.bookstore.domain.model.Banner;
import vn.com.bookstore.domain.model.Book;
import vn.com.bookstore.domain.model.SupportNews;
import vn.com.bookstore.service.AudioBookService;
import vn.com.bookstore.service.BannerService;
import vn.com.bookstore.service.BooksOnlineService;
import vn.com.bookstore.service.SupportNewsService;

@Controller
public class ImagesController {

	private static final Logger logger = Logger.getLogger(ImagesController.class);
	@Autowired
	public BooksOnlineService booksOnlineService;
	
	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private AudioBookService audioBookService;
	
	@Autowired
	private SupportNewsService supportNewsService;
	
	@Value("${upload.ebooks.url.server}")
	private String urlBookImg;
	
	@Value("${upload.banners.url.server}")
	private String urlBannerImg;
	
	@Value("${upload.auio.image.url.server}")
	private String auioImageUrl;
	
	@Value("${url.support.news.images}")
	private String supportNewsUrl;
	
	@RequestMapping(value = "online/getBookImage", method = RequestMethod.GET)
	public void getImages(@RequestParam("bookId") int bookId,
			HttpServletResponse response, HttpServletRequest request) {
		Book book = booksOnlineService.getBookById(bookId);
		String uri = urlBookImg + book.getUrlImageFile();
		String contentType = "image/*";
		File file = new File(uri);
		try {
			response.setContentType(contentType);
			FileCopyUtils.copy(new FileInputStream(file),
					response.getOutputStream());
		} catch (Exception e) {
			logger.error(e);
		} 
	}
	
	@RequestMapping(value = "online/getAudioImage", method = RequestMethod.GET)
	public void getAudioImages(@RequestParam("id") int id,
			HttpServletResponse response, HttpServletRequest request) {
		AudioBook book = audioBookService.getAudioBookById(id);
		String uri = auioImageUrl + book.getImageUrl();
		String contentType = "image/*";
		File file = new File(uri);
		try {
			response.setContentType(contentType);
			FileCopyUtils.copy(new FileInputStream(file),
					response.getOutputStream());
		} catch (Exception e) {
			logger.error(e);
		} 
	}
	
	@RequestMapping(value = "online/getBannerImage", method = RequestMethod.GET)
	public void getBannerImages(@RequestParam("bannerId") int bannerId,
			HttpServletResponse response, HttpServletRequest request) {
		Banner banner = bannerService.getBannerDetail(bannerId);
		String uri = urlBannerImg + banner.getUrlFile();
		String contentType = "image/*";
		File file = new File(uri);
		try {
			response.setContentType(contentType);
			FileCopyUtils.copy(new FileInputStream(file),
					response.getOutputStream());
		} catch (Exception e) {
			logger.error(e);
		} 
	}

	@RequestMapping(value = "online/getSupportImage", method = RequestMethod.GET)
	public void getSupportImages(@RequestParam("supportId") int id,
			HttpServletResponse response, HttpServletRequest request) {
		SupportNews supportNews = supportNewsService.getSupportNewsById(id);
		String uri = supportNewsUrl + supportNews.getUrlImage();
		String contentType = "image/*";
		File file = new File(uri);
		try {
			response.setContentType(contentType);
			FileCopyUtils.copy(new FileInputStream(file),
					response.getOutputStream());
		} catch (Exception e) {
			logger.error(e);
		} 
	}
}
